package com.webchat.ugc.messaegqueue.consumer.redis;

import com.webchat.common.enums.messagequeue.MessageQueueEnum;
import com.webchat.common.service.messagequeue.consumer.AbstractRedisQueueConsumer;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.dto.queue.MomentPublishMessageDTO;
import com.webchat.ugc.messaegqueue.service.MomentPublishConsumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(value = false)
@Slf4j
public class MomentPublishRedisMQConsumer extends AbstractRedisQueueConsumer<MomentPublishMessageDTO> {


    @Autowired
    private MomentPublishConsumeService momentPublishConsumeService;

    public void initBean() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                MomentPublishRedisMQConsumer.this.schedule();
            }
        }).start();
    }

    @Override
    protected MomentPublishMessageDTO convert(String s) {

        return JsonUtil.fromJson(s, MomentPublishMessageDTO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_MOMENT_PUBLISH;
    }

    @Override
    protected void receive(MomentPublishMessageDTO data) {

        /**
         * 动态发布后置实现流程
         */
        momentPublishConsumeService.consume(data.getMoment());
    }

    @Override
    protected void error(MomentPublishMessageDTO data, Exception ex) {
        // TODO
    }
}
